package com.zhang.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.zhang.yygh.cmn.client.DictFeignClient;
import com.zhang.yygh.hosp.repository.HospitalRepository;
import com.zhang.yygh.hosp.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    //上传医院信息
    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        String string = JSONObject.toJSONString(paramMap);
        Hospital paramHospital = JSONObject.parseObject(string, Hospital.class);
        String hoscode = (String) paramMap.get("hoscode");
        Hospital hospital=hospitalRepository.findByHoscode(hoscode);
        if (hospital!=null){ //更新操作
            paramHospital.setCreateTime(hospital.getCreateTime());
            paramHospital.setUpdateTime(new Date());
            paramHospital.setIsDeleted(hospital.getIsDeleted());
            paramHospital.setStatus(hospital.getStatus());
            paramHospital.setId(hospital.getId());
            hospitalRepository.save(paramHospital);
        }else { //添加操作
            paramHospital.setStatus(0);
            paramHospital.setCreateTime(new Date());
            paramHospital.setUpdateTime(new Date());
            paramHospital.setIsDeleted(0);
            hospitalRepository.save(paramHospital);
        }
    }

    @Override
    public Map<String, Object> getHospitalByHoscode(String hoscode) {
        Hospital byHoscode = hospitalRepository.findByHoscode(hoscode);
        String string = JSONObject.toJSONString(byHoscode);
        Map map = JSONObject.parseObject(string, Map.class);
        return map;
    }

    @Override
    public Page<Hospital> getHospitalPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.ASC, "createTime"));
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital); //把A对象的属性值copy到另一个对象对应的属性里
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> all = hospitalRepository.findAll(example, pageRequest);
        all.getContent().parallelStream().forEach(item->{
            this.packageHospital(item);
        });
        return all;
    }

    //修改医院状态
    @Override
    public void updateStatus(String id, Integer status) {
        if (status==0||status==1){
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    //根据医院id查询医院的详细信息
    @Override
    public Hospital getHospitalDetailByid(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.packageHospital(hospital);
        return hospital;
    }

    //根据医院名字进行模糊查询
    @Override
    public List<Hospital> findByName(String name) {
        List<Hospital> hospitalList= hospitalRepository.findByHosnameLike(name);
        return hospitalList;
    }

    //根据医院编号,查询医院详细信息
    @Override
    public Hospital findByHoscode(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }

    //通过省市区编号去字典表中查汉字信息
    private void packageHospital(Hospital item) {
        //省编号得到的对应的地址值
        String proviceAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(item.getProvinceCode()));
        //市编号得到的对应的地址值
        String cityAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(item.getProvinceCode()));
        //区编号得到的对应的地址值
        String dictAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(item.getProvinceCode()));
        //医院等级
        String hospitalLevel=dictFeignClient.getAddressNameByValue(DictEnum.HOSTYPE.getDictCode(),Long.valueOf(item.getHostype()));
        item.getParam().put("hospitalLevel", hospitalLevel);
        item.getParam().put("fullAddress", proviceAddress + cityAddress + dictAddress + item.getAddress());
        //hostype:1
        //provinceCode=11000
    }
}
